/**   
 * @Title: Field.java 
 * @Package org.playgod1984.du.ddesign.model 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author playgod playgod1984@163.com  
 * @date 2014年10月27日 下午3:24:05 
 * @version V1.0   
 */
package org.playgod1984.du.ddesign.model;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.playgod1984.du.base.util.DuDesignUtil;

import com.jfinal.log.Log4jLogger;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/** 
 * @ClassName: Field 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author playgod playgod1984@163.com 
 * @date 2014年10月27日 下午3:24:05 
 *  
 */
@SuppressWarnings("all")
public class Field extends Model<Field> {
	
	private static final Logger log = Log4jLogger.getLogger(Field.class);
	public static final Field dao = new Field();
	/**
	 * @Title: getFieldsWithPagenate
	 * @author:playgod playgod1984@163.com 
	 * @Description: 分页 
	 * @param @param params
	 * @param @return    设定文件 
	 * @return Page<Field>    返回类型 
	 * @throws
	 */
	public Page<Field> getFieldsWithPagenate(Map params){
		int pageNumber = NumberUtils.toInt((String)params.get("page"));
		int pageSize = NumberUtils.toInt((String)params.get("pagesize"));
		String sql_where=" where tableid='" + params.get("tableid") + "'";
//				+ "and tableid='"+params.get("tableid")+"'";
		String sqlselect="SELECT * ";
		return paginate(pageNumber, pageSize, sqlselect, "FROM dud_design_field fie" + sql_where);
	}
	/**
	 * 
	 * @Title: saveUpdate
	 * @author:playgod playgod1984@163.com 
	 * @Description: 保存更新成功 
	 * @param @param params
	 * @param @return    设定文件 
	 * @return boolean    返回类型 
	 * @throws
	 */
	public boolean saveUpdate(Map params){
		String dataEditString = (String)params.get("dataEdit");
		List<Record> rList  = DuDesignUtil.json2ListOfMap(dataEditString);
		for(int i=0;i < rList.size();i++){
			Record r = rList.get(i);
			r.set("updator", params.get("currentUser"));
			r.set("updatetime",new Date());
			Db.update("dud_design_field", "fieldid", r);
		}
		return true;
	}
	/**
	 * @Title: saveAdd
	 * @author:playgod playgod1984@163.com 
	 * @Description: 保存列 
	 * @param @param params
	 * @param @return    设定文件 
	 * @return boolean    返回类型 
	 * @throws
	 */
	public boolean saveAdd(Map params){
		String dataEditString = (String)params.get("dataAdd");
		List<Record> rList  = DuDesignUtil.json2ListOfMap(dataEditString);
		for(int i=0;i < rList.size();i++){
			Record r = rList.get(i);
			r.set("creator", params.get("currentUser"));
			r.set("createtime",new Date());
			r.set("updator", params.get("currentUser"));
			r.set("updatetime",new Date());			
			Db.save("dud_design_field",  r);
		}
		return true;
	}
	/**
	 * 
	 * @Title: deleteRows
	 * @author:playgod playgod1984@163.com 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 
	 * @param @param params
	 * @param @return    设定文件 
	 * @return int    返回类型 
	 * @throws
	 */
	public int deleteRows(Map params){
		int results =0;
		try{
			String[] ids = StringUtils.split((String)params.get("keys"),",");
			for(int i=0;i<ids.length;i++){
				if(dao.deleteById(ids[i])){
					results++;
				}
			}
			return results;
		}catch(Exception ex){
			log.error(ex.getMessage());
			return -1;
		}
	}
}
